//
//  MBTheNewICell.m
//  MiBaoTu
//
//  Created by mac004 on 2016/10/11.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBTheNewICell.h"

@implementation MBTheNewICell
-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self onCreatUI];
    }
    return self;
}
-(void)onCreatUI{
  
    //大图
    self.bigImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    [self.contentView addSubview:self.bigImageView];
    //背景框
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, self.height/700*520, self.width, self.height/700*180)];
    view.backgroundColor=[UIColorFromHex(0x000000) colorWithAlphaComponent:.6];
    _bigImageView.layer.cornerRadius = 5.;
    _bigImageView.layer.masksToBounds = YES;


    [self.bigImageView addSubview:view];
    //图像
    self.iconImageView=[[UIImageView alloc]initWithFrame:CGRectMake(50*PERCEN, 20*PERCEN, view.height*2/3, view.height*2/3)];
    self.iconImageView.layer.cornerRadius=self.iconImageView.height/2;
    self.iconImageView.layer.masksToBounds=YES;
    [view addSubview:self.iconImageView];
   
       //大标题
    self.bigTitle=[[UILabel alloc]initWithFrame:CGRectMake(self.iconImageView.right+20*PERCEN, 30*PERCEN, 320*PERCEN, 30*PERCEN)];
    self.bigTitle.font=[UIFont systemFontOfSize:36*PERCEN];
    self.bigTitle.textColor=UIColorFromHex(0xffffff);
    [view addSubview:self.bigTitle];
    //小标题
    self.smallTitle=[[UILabel alloc]initWithFrame:CGRectMake(self.bigTitle.left, self.bigTitle.bottom+30*PERCEN, 320*PERCEN, 30*PERCEN)];
    self.smallTitle.font=[UIFont systemFontOfSize:30*PERCEN];
    self.smallTitle.textColor=UIColorFromHex(0xffffff);
    [view addSubview:self.smallTitle];
    
    
}
- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
    [self setNeedsDisplay];
}
-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic=dataDic;
    NSLog(@"_aaa==%@",_dataDic);
    NSString*CoverPath=_dataDic[@"CoverPath"];
    if ([CoverPath isKindOfClass:[NSNull class]]) {
        self.bigImageView.image=[UIImage imageNamed:@"morenfm_qzx_3.0"];
    }else{
        if (CoverPath.length==0) {
            self.bigImageView.image=[UIImage imageNamed:@"morenfm_qzx_3.0"]; 
        }else{
        [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,dataDic[@"CoverPath"]]] placeholderImage:[UIImage imageNamed:@"morenfm_qzx_3.0"] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _bigImageView.image = [self getCenterImg:image];
        }];
        }

    }
    
    if ([dataDic[@"FaceImg"] isKindOfClass:[NSNull class]]) {
        self.iconImageView.image=[UIImage imageNamed:@"touxiang_3.0"];
    }else{
        
        [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,dataDic[@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"touxiang_3.0"] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _iconImageView.image = [self getCenterImg:image];
        }];

        
        
//      [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:
//                                                [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,  dataDic[@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"touxiang_3.0"]];
    }
    self.bigTitle.text=dataDic[@"NickName"];
    self.smallTitle.text=dataDic[@"Sentiment"];
   

}
#pragma mark--修正图片

- (UIImage *)getCenterImg:(UIImage *)image
{
    CGImageRef imageRef = image.CGImage;
    float imgHeight = 1.0 * image.size.width * _bigImageView.height/_bigImageView.width;
    float rectHeight = imgHeight;
    float top = (image.size.height-imgHeight)/2.;
    CGRect rect;
    if (top > 0) {
        //图片实际高度比UI高度高
        if (imgHeight > image.size.height) {
            rectHeight = image.size.height;
            top = (imgHeight - image.size.height)/2.;
        }
        rect = CGRectMake(0, top, image.size.width, rectHeight);
    }
    else {
        //UI高度比图片实际高度高
        float imgWidth = 1.0 * image.size.height * _bigImageView.width/_bigImageView.height;
        float rectWidth = imgWidth;
        float left = (image.size.width-imgWidth)/2.;
        if (imgWidth > image.size.width) {
            rectWidth = image.size.width;
            left = (imgWidth - image.size.width)/2.;
        }
        rect = CGRectMake(left, 0, rectWidth, image.size.height);
    }
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    return rectImg;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}

@end
